<script type="text/html" template lay-done="layui.data.sendParams(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-CoreCmsUser-editForm" id="LAY-app-CoreCmsUser-editForm">
        <input type="hidden" name="id" id="id" value="{{d.params.data.model.id || '' }}" />

        <!--<blockquote class="layui-elem-quote">
            目前本系统暂无密码加账号功能，但是预留了相关字段，可自行对接。
        </blockquote>-->
        <div class="layui-form-item">
            <div class="layui-inline  layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">来源：</label>
                <div class="layui-input-inline">
                    <select name="sourceId" lay-verify="required">
                        <option value="" selected="selected">请选择</option>
                        {{# layui.each(d.params.data.sourceList, function(index, item){ }}
                        <option value="{{ item.id }}" {{ item.id == d.params.data.model.sourceId ? 'selected="selected"' : '' }}>{{ item.userName }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline  layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">客户分类：</label>
                <div class="layui-input-inline">

                    <select name="customerClassId">
                        <option value="" selected="selected">请选择</option>
                        {{# layui.each(d.params.data.classList, function(index, item){ }}
                        <option value="{{ item.id }}" {{ item.id == d.params.data.model.customerClassId ? 'selected="selected"' : '' }}>{{ item.className }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">客户名字：</label>
                <div class="layui-input-inline">
                    <input type="text" name="userName" required lay-verify="required" class="layui-input" lay-affix="clear" value="{{d.params.data.model.userName || '' }}">
                </div>
            </div>
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">手机号：</label>
                <div class="layui-input-inline">
                    <input type="text" name="mobile" required lay-verify="required|phone" class="layui-input" lay-affix="clear" value="{{d.params.data.model.mobile || '' }}">
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-input-wrap layui-input-wrap-prefix">
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label">备用手机：</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" class="layui-input" lay-affix="clear" value="{{d.params.data.model.phone || '' }}">
                </div>
            </div>
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label">客户微信号：</label>
                <div class="layui-input-inline">
                    <input type="text" name="customerWx" class="layui-input" lay-affix="clear" value="{{d.params.data.model.customerWx || '' }}">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">性别：</label>
                <div class="layui-input-inline">

                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <select name="sex" required>
                        <option value="1" {{d.params.data.model.sex ===1 ? 'selected="selected"':'' }}>男</option>
                        <option value="2" {{d.params.data.model.sex ===2 ? 'selected="selected"':'' }}>女</option>
                        <option value="3" {{d.params.data.model.sex ===3 ? 'selected="selected"':'' }}>未知</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">加粉日期：</label>
                <div class="layui-input-inline">
                    <input type="text" name="birthday" id="editTime-birthday" class="layui-input" value="{{ d.params.data.model.birthday ? layui.util.toDateString(d.params.data.model.birthday, 'yyyy-MM-dd') : '' }}">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">客户编号：</label>
                <div class="layui-input-inline">
                    <input type="text" name="userName" class="layui-input" readonly="readonly" value="{{d.params.data.model.number || '' }}">
                </div>
            </div>
            <div class="layui-inline  layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">所属销售：</label>
                <div class="layui-input-inline">
                    <select name="sysid" lay-verify="required">
                        <option value="" selected="selected">请选择</option>
                        {{# layui.each(d.params.data.uuserList, function(index, item){ }}
                        <option value="{{ item.id }}" {{ item.id == d.params.data.model.id ? 'selected="selected"' : '' }}>{{ item.userName }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
        </div>
        <!--<div class="layui-form-item">
            <label for="name" class="layui-form-label">客户编号</label>
            <div class="layui-input-block">
                <input name="number" id="number" class="layui-input" value="{{d.params.data.model.number || '' }}" readonly="readonly" />
            </div>

        </div>-->
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">旧地址</label>
            <div class="layui-input-block">
                <input name="address" id="address" class="layui-input" value="{{d.params.data.model.address || '' }}" />
            </div>

        </div>
        <div class="layui-form-item">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">选择地区</label>
                    <div class="layui-input-inline">
                        <select name="province" lay-filter="province">
                            <option value="">请选择省</option>
                        </select>
                    </div>
                    <div class="layui-input-inline" style="display: none;">
                        <select name="city" lay-filter="city">
                            <option value="">请选择市</option>
                        </select>
                    </div>
                    <div class="layui-input-inline" style="display: none;">
                        <select name="area" lay-filter="area">
                            <option value="">请选择县/区</option>
                        </select>
                    </div>
                </div>
                <label for="name" class="layui-form-label">新地址</label>
                <div class="layui-input-block">
                    <input name="newaddress" id="newaddress" class="layui-input" lay-reqText="请输入地址" placeholder="请输入地址" />
                </div>
                <!--<input id="address" type="text" name="addressc" class="layui-input" lay-affix="clear">>-->
            </form>

            <!--<label for="name" class="layui-form-label layui-form-required">地址</label>
            <div class="layui-input-block">
                <input name="address" id="address"  class="layui-input" lay-reqText="请输入地址" placeholder="请输入地址" />
            </div>>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-CoreCmsUser-orderForm-submit" id="LAY-app-CoreCmsUser-orderForm-submit" value="保存客户资料">
            <!--<input type="button" class="layui-btn" lay-active="goods_show" value="添加产品">-->
        </div>

        <div class="layui-form-item">
            <div>
                <button class="layui-btn layui-btn-sm" lay-active="goodsShow">添加产品</button>
                <input type="hidden" name="goodsId" id="goodsId" value="" class="layui-input">
                <input type="hidden" name="productsCount" id="productsCount" value="0">
            </div>

            <table class="layui-table" lay-size="sm">
                <colgroup>
                    <col width="55">
                    <col>
                    <col width="60">
                    <col width="150">
                    <col width="50">
                    <col width="60">
                    <col width="100">
                    <col width="100">
                    <col width="80">
                    <col width="60">
                </colgroup>
                <thead>
                    <tr>
                        <th>序列</th>
                        <th>商品名称</th>
                        <th>规格图</th>
                        <th>规格名称</th>
                        <th>库存</th>
                        <th>预设销售价</th>
                        <th>实际销售价</th>
                        <th>当前库存</th>
                        <th>顺序</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="productsTbody">
                </tbody>
            </table>
        </div>


        <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title">
                <li class="layui-this">标签1</li>
                <li>标签2</li>
                <li>标签3</li>
                <li>标签4</li>
                <li>标签5</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">内容-1</div>
                <div class="layui-tab-item">内容-2</div>
                <div class="layui-tab-item">内容-3</div>
                <div class="layui-tab-item">内容-4</div>
                <div class="layui-tab-item">内容-5</div>
            </div>
        </div>



        <div id="LAY-popup-CoreCmsUser-EditPoint"></div>
    </div>

</script>



<script id="get_tpl" type="text/html">
    {{# layui.each(d.list, function(index, item){ }}
    <tr>
        <td>
            {{item.id}}
        </td>
        <td>
            <input type="hidden" value="0" name="product[id][]" />
            <input type="hidden" value="{{item.id}}" name="product[productId][]" />
            <input type="hidden" value="{{item.goodsId}}" name="product[goodId][]" />
            <input type="hidden" value="0" name="product[solitaireId][]" />
            {{item.name}}
        </td>
        <td>
            <img src="{{ item.images && item.images !='null'? item.images : layui.setter.noImagePicUrl }}" id="productImagesSrc" style="width:40px;height:40px">
        </td>
        <td>
            {{item.spesDesc || '默认规格'}}
        </td>
        <td>
            {{item.stock}}
        </td>
        <td>
            ¥{{item.price}}
        </td>
        <td>
            <input type="text" name="product[price][]" value="{{item.price}}" placeholder="实际销售价" class="layui-input layui-inline-1">
        </td>
        <td>
            {{item.stock}}
        </td>
        <td>
            <input type="text" name="product[sortId][]" value="{{1}}" placeholder="排序" class="layui-input layui-inline-1">
        </td>
        <td>
            <a class="layui-btn layui-btn-danger layui-btn-xs del-class" id="{{item.id}}" lay-active="goodsDelete">删除</a>
        </td>
    </tr>
    {{# }); }}
</script>



<script src="../../../lib/jquery/address.js"></script>
<script>var areaData = Area;</script>
<script>
    var ids = {};

    layui.data.sendParams = function (d) {
        console.log(d);

        layui.use(['admin', 'form', 'laydate', 'upload', 'coreHelper', 'cropperImg', 'view', 'util', 'laytpl'],

            function () {
                var $ = layui.$
                    , form = layui.form
                    , admin = layui.admin
                    , laydate = layui.laydate
                    , upload = layui.upload
                    , cropperImg = layui.cropperImg
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , laytpl = layui.laytpl,
                    view = layui.view;

                var selectedProvince = '';
                var selectedCity = '';
                var selectedArea = '';

                $form = $('form');
                loadProvince();
                // 加载省数据
                function loadProvince() {
                    var proHtml = '<option value="">请选择省</option>';
                    for (var i = 0; i < areaData.length; i++) {
                        proHtml += '<option value="' + areaData[i].provinceCode + '_' + areaData[i].mallCityList.length + '_' + i + '">' + areaData[i].provinceName + '</option>';
                    }
                    $form.find('select[name=province]').html(proHtml);
                    form.render();

                    form.on('select(province)', function (data) {
                        selectedProvince = data.elem.options[data.elem.selectedIndex].text;
                        selectedCity = ''; // 重置市
                        selectedArea = ''; // 重置区

                        $form.find('select[name=area]').html('<option value="">请选择县/区</option>').parent().hide();
                        var value = data.value;
                        var d = value.split('_');
                        var count = d[1];
                        var index = d[2];

                        if (count > 0) {
                            loadCity(areaData[index].mallCityList);
                        } else {
                            $form.find('select[name=city]').parent().hide();
                        }
                        updateAddress();
                    });
                }

                // 加载市数据
                function loadCity(citys) {
                    var cityHtml = '<option value="">请选择市</option>';
                    for (var i = 0; i < citys.length; i++) {
                        cityHtml += '<option value="' + citys[i].cityCode + '_' + citys[i].mallAreaList.length + '_' + i + '">' + citys[i].cityName + '</option>';
                    }
                    $form.find('select[name=city]').html(cityHtml).parent().show();
                    form.render();

                    form.on('select(city)', function (data) {
                        selectedCity = data.elem.options[data.elem.selectedIndex].text;
                        selectedArea = ''; // 重置区

                        var value = data.value;
                        var d = value.split('_');
                        var count = d[1];
                        var index = d[2];

                        if (count > 0) {
                            loadArea(citys[index].mallAreaList);
                        } else {
                            $form.find('select[name=area]').parent().hide();
                        }
                        updateAddress();
                    });
                }

                // 加载区数据
                function loadArea(areas) {
                    var areaHtml = '<option value="">请选择县/区</option>';
                    for (var i = 0; i < areas.length; i++) {
                        areaHtml += '<option value="' + areas[i].areaCode + '">' + areas[i].areaName + '</option>';
                    }
                    $form.find('select[name=area]').html(areaHtml).parent().show();
                    form.render();

                    form.on('select(area)', function (data) {
                        selectedArea = data.elem.options[data.elem.selectedIndex].text;
                        updateAddress();
                    });
                }

                // 更新地址输入框
                function updateAddress() {
                    $('#newaddress').val(
                        (selectedProvince ? selectedProvince + ' ' : '') +
                        (selectedCity ? selectedCity + ' ' : '') +
                        (selectedArea ? selectedArea : '')
                    );
                }
                //添加产品
                $('#LAY-app-CoreCmsUser-addOrderForm-submit').click(function () {
                    coreHelper.Post("Api/CoreCmsUser/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '产品列表',
                                area: ['800px', '800px'],
                                id: 'LAY-app-CoreCmsnewAddOrder-createForm',
                                success: function (layero, index) {
                                    view(this.id).render('order/orders/add', { data: e.data }).done(function () {
                                        console.log("sdfsa", e.data);
                                        //监听提交

                                    });
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsDepartment-addcreateForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                });

                //封面图图片上传
                $('#upBtn').click(function () {
                    cropperImg.cropImg({
                        aspectRatio: 1 / 1,
                        imgSrc: $('#viewImgBox').attr('src'),
                        onCrop: function (data) {
                            var loadIndex = layer.load(2);
                            coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                                if (0 === res.code) {
                                    $('#viewImgBox').attr('src', res.data.fileUrl);
                                    $("#avatarImage").val(res.data.fileUrl);
                                    layer.msg(res.msg);
                                    layer.close(loadIndex);
                                } else {
                                    layer.close(loadIndex);
                                    layer.msg(res.msg, { icon: 2, anim: 6 });
                                }
                            });
                        }
                    });
                });

                laydate.render({
                    elem: '#editTime-birthday',
                });


                //处理属性 为 lay-active 的所有元素事件
                util.event('lay-active', {
                    goodsShow: function () {
                        admin.popup({
                            shadeClose: false,
                            title: '选择商品',
                            area: ['900px', '80%'],
                            id: 'LAY-app-CoreCmsPinTuanRule-createForm-GetGoodsList',
                            success: function (layero, index) {
                                view(this.id).render('common/getProductsIds', null).done(function () {
                                    form.render();
                                    //监听商品列表页工具条
                                    form.on('submit(LAY-app-CoreCmsProducts-getData)',
                                        function (data) {
                                            if (Object.getOwnPropertyNames(ids).length === 0) {
                                                layer.msg("请先选择商品");
                                                return;
                                            }
                                            //判断个数是否满足
                                            if (Object.getOwnPropertyNames(ids).length > 10) {
                                                layer.msg("最多只能选择" + 10 + "个");
                                                return false;
                                            }

                                            var the_val = "";
                                            var count = 0;
                                            for (var key in ids) {
                                                the_val += "," + key;
                                                count++;
                                            }
                                            $("#goodsId").val(the_val.slice(1));
                                            $('#productsCount').val(count);

                                            var getTpl = get_tpl.innerHTML, view = document.getElementById('productsTbody');
                                            var tmpData = {};
                                            tmpData.list = ids;
                                            laytpl(getTpl).render(tmpData, function (html) {
                                                view.innerHTML = html;
                                            });
                                            form.render();
                                            layer.close(index);
                                        });

                                });
                            }
                        });
                    },
                    goodsDelete: function () {
                        var idsArray = $("#goodsId").val().split(",");
                        for (var i = 0; i < idsArray.length; i++) {
                            var id = $(this).attr("id");
                            if (idsArray[i] == id) {
                                idsArray.splice(i, 1);
                            }
                            if (ids[id]) {
                                delete ids[id];
                            }
                        }
                        $("#goodsId").val(idsArray.join(","));
                        $('#productsCount').val(idsArray.length);
                        $(this).parent().parent('tr').remove();
                        form.render();
                    }
                });




                form.verify({
                    verifyuserName: [/^[\S]{0,20}$/, '用户名最大只允许输入20位字符，且不能出现空格'],
                    verifypassWord: [/^[\S]{0,50}$/, '密码最大只允许输入50位字符，且不能出现空格'],
                    verifymobile: [/^[\S]{0,15}$/, '手机号最大只允许输入15位字符，且不能出现空格'],
                    verifyavatarImage: [/^[\S]{0,255}$/, '头像最大只允许输入255位字符，且不能出现空格'],
                    verifynickName: [/^[\S]{0,50}$/, '昵称最大只允许输入50位字符，且不能出现空格'],
                });
                //重载form
                form.render(null, 'LAY-app-CoreCmsUser-editForm');
            })
    };
</script>